<?php
namespace app\index\controller;

use app\index\controller\BaseController;
use app\index\model\User;
use think\facade\Db;
use think\facade\Env;
use think\api\Client;
use think\facade\View;
class Xia extends BaseController
{
    public function index()
    {

        $mall_config=Db::name('mall_config')->column('value','key');
        $last=Db::name('mall_update')->where('isopen',1)->order('app_v desc')->find();
		return View::fetch('',[
			'app_v'=>$last['app_v'],
			'app_desc'=>$mall_config['app_desc'],
			'app_name'=>$mall_config['app_name'],
            'app_logo'=> $mall_config['app_logo']
		]);
    }
    public function ajax(){

        $data=input('param.');
        if($data['type']=='get_yzm'){
            $msg=mt_rand(111111, 999999);
            $user=new User();
            if ($this->sendsmg($data['phone'], $msg)) {
                if ($u=Db::name('user')->where('phone', $data['phone'])->find()) {
                    $user->where('id', $u['id'])->update([
                        'yzm'=>$msg
                    ]);
                } else {
                    $user->save([
                        'phone'=>$data['phone'],
                        'yzm'=>$msg,
                    ]);
                }
                return json(['code'=>1,'message'=>'成功']);
            } else {
                return json(['code'=>0,'message'=>'发送验证码失败']);
            }
        }
    }
    public function main()
    {
          if(request()->isPost()){
            $data=input('post.');
            if(!isset($data['code'])){
                $this->error('邀请码不正确');
            }
            if(!isset($data['password']) or !$data['password']){
                $this->error('请填写密码');
            }
            if($data['password']!=$data['password2']){
                $this->error('两次密码不一致');
            }
            $f_user=Db::name('user')->where('user_no',$data['code'])->find();
            if(!$f_user){
                $this->error('邀请码不正确');
            }
            if ($wo=Db::name('user')->where('phone', $data['phone'])->where('yzm',$data['yzm'])->where('isopen',1)->find()) {
                if (!$wo['user_no']) {
                    Db::name('user')->where('id', $wo['id'])->update(['user_no'=>$this->sui($wo['id'])]);
                }
                if (!$wo['fid']) {
                    Db::name('user')->where('id', $wo['id'])->update(['fid'=>$f_user['id']]);
                }
                Db::name('user')->where('id', $wo['id'])->update(['password'=>md5(sha1($data['password']))]);
                $this->success('注册成功，请下载app',url('xia/index',['st'=>1]));
            } else {
                $this->error('注册失败了');
            }
        }
        $mall_config=Db::name('mall_config')->column('value','key');
        $last=Db::name('mall_update')->where('isopen',1)->order('app_v desc')->find();
        return View::fetch('',[
            'app_v'=>$last['app_v'],
            'app_desc'=>$mall_config['app_desc'],
            'app_name'=>$mall_config['app_name'],
            'app_logo'=> $mall_config['app_logo']
        ]);
    }
    public function shuoming(){
        return View::fetch('',[
            'data'=>Db::name('shuoming')->where('id',input('id'))->find()
        ]);
    }
}
